package com.study.talk.ui.app.register

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.study.common.ToastUtil
import com.study.common.VerificationUtil
import com.study.talk.ui.app.base.BaseViewModel
import com.study.talk.data.model.User
import com.study.talk.data.model.vo.RegisterUserVo
import com.study.talk.netty.NettyClient
import com.study.talk.netty.message.request.SmsSendRequest
import com.study.talk.netty.message.response.SmsSendResponse
import com.study.talk.utils.extras.useless
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class RegisterViewModel @Inject constructor(application: Application) : BaseViewModel(application = application) {

    val username = MutableLiveData("")
    val phone = MutableLiveData("")
    val code = MutableLiveData("")

    val password = MutableLiveData("")
    val passwordAgain = MutableLiveData("")

    val complete = MutableLiveData<User>()

    val fail = MutableLiveData<String>()


    fun receiverMsg(response: SmsSendResponse) {
        username.postValue(response.username)
        phone.postValue(response.phoneNumber)
        code.postValue(response.code)
    }

    fun phoneCheck(): Boolean {
        if (username.value?.useless(app, "请输入用户名") == true) return false
        if (phone.value?.useless(app, "请输入手机号码") == true) return false

        if (!VerificationUtil.isValidTelNumber(phone.value!!)) {
            ToastUtil.showToast(app, "请输入正确的手机号码")
            return false
        }

        if (username.value!!.length < 6) {
            ToastUtil.showToast(app, "用户名长度不能小于6")
            return false
        }

        if (!VerificationUtil.isValidUserName(username.value!!)) {
            ToastUtil.showToast(app, "请输入正确的手机号码")
            return false
        }
        return true
    }

    fun phoneSubmit() {
        NettyClient.sendMessage(
            SmsSendRequest(
                username.value?.trim(), phone.value?.trim()
            )
        )
    }

    fun passwordCheck(): Boolean {
        if (password.value?.useless(app, "请输入密码") == true) return false
        if (passwordAgain.value?.useless(app, "请输入确认密码") == true) return false

        if (password.value!!.length < 8) {
            ToastUtil.showToast(app, "密码长度必须大于8位")
            return false
        }

        if (password.value != passwordAgain.value) {
            ToastUtil.showToast(app, "两次密码不一致")
            return false
        }
        return true
    }

    fun passwordSubmit() {
        viewModelScope.launch {
            val registerUserVo =
                RegisterUserVo(
                    username.value,
                    password.value,
                    phone.value
                )
            val result = userRepository.registerUser(registerUserVo) {
                ToastUtil.showToast(app, it)
                fail.postValue(it)
            }
            result?.let { complete.postValue(it) }
        }
    }
}